/**
 * Author(s): xuming.Wong <xuming.Wong@gmail.com>
 */

#ifndef __QS_TransactionDatabase_H__
#define __QS_TransactionDatabase_H__

#include "Transaction.h"

#include <map>
#include <string>
#include <sstream>

#include <QtCore/QByteArray>

namespace QSThink
{

	//A symbol's all transactions by day
	class StockTransactions
	{

	public:
		StockTransactions(QString symbol);
		Transaction1Day* get(int day);
		
		int getOpenDayCount(int startDay,int endDay);


		Transaction1Day* getDay(int day);
		void loadTreeData();

		double getHighestPriceEx(QDate startDate,QDate endDate);
		double getLowestPriceEx(QDate startDate,QDate endDate);
	protected:
		void buidTreeData();
	protected:
		QString _symbol;
		QDate _startDate;
		QDate _endDate;
		bool _loaded;//

		std::map<int,Transaction1Day*> _transactions;//first is day

		/*
		double _highPriceEx;
		double _lowestPriceEx;
		double _balancedPriceEx;
		*/
	};

	
	/*manipulate stock
	*/
	class TransactionDatabase
	{
	public:
		//construct string id
		//symbol_day
		typedef std::map<QString,StockTransactions*> TransactionsMap;
	protected:
		TransactionDatabase();
	public:
		static TransactionDatabase& getSingleton()
		{
			static TransactionDatabase inst;
			return inst;
		}

		StockTransactions* getTransactions( QString symbol);


		

		//get the start date of the stock that have data by symbol
		QDate getStartDate(QString symbol);

		//get the end date of the stock that have data by symbol
		QDate getEndDate(QString symbol);

		//get the start date of the stock that have data by symbol
		int getStartDay(QString symbol);

		//get the end date of the stock that have data by symbol
		int getEndDay(QString symbol);


		Transaction1Day* getDayTransaction(QString symbol,QDate date);

		Transaction1Day* getDayTransaction(QString symbol,int day);



		//DayTransaction* getDayTransaction(int day,int symbol);

		//simple text based format
		//DayTransaction* addDayTransaction(int day,QString symbol,QByteArray bytes);

	protected:
		//Transaction* parseLine( QString line );

	protected:
		TransactionsMap _transactions;

		
	};
}

#endif
